<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>羊了个羊简易版</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            text-align: center;
        }

        #game-board {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            max-width: 600px;
            margin: 0 auto;
        }

        .card {
            width: 80px;
            height: 80px;
            background-color: #ccc;
            margin: 5px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
        }

        #selected-cards {
            margin-top: 20px;
        }

        .selected {
            display: inline-block;
            width: 40px;
            height: 40px;
            background-color: #ddd;
            margin: 5px;
            font-size: 18px;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <h1>羊了个羊简易版</h1>
    <div id="game-board">
        <?php
        // 生成随机的卡片布局
        $cards = [];
        $cardTypes = ['🐏', '🐑', '🐏', '🐑', '🐏', '🐑', '🐏', '🐑', '🐏', '🐑'];
        shuffle($cardTypes);
        foreach ($cardTypes as $card) {
            echo '<div class="card" data-card="' . $card . '">' . $card . '</div>';
        }
        ?>
    </div>
    <div id="selected-cards"></div>
    <script>
        const gameBoard = document.getElementById('game-board');
        const selectedCardsDiv = document.getElementById('selected-cards');
        const selectedCards = [];

        gameBoard.addEventListener('click', function (event) {
            if (event.target.classList.contains('card')) {
                const card = event.target.dataset.card;
                selectedCards.push(card);
                const selectedCardElement = document.createElement('div');
                selectedCardElement.classList.add('selected');
                selectedCardElement.textContent = card;
                selectedCardsDiv.appendChild(selectedCardElement);

                // 检查是否有三个相同的卡片
                if (selectedCards.length >= 3) {
                    const lastThree = selectedCards.slice(-3);
                    if (lastThree.every(c => c === lastThree[0])) {
                        // 发送请求到后端进行消除处理
                        const xhr = new XMLHttpRequest();
                        xhr.open('POST', 'eliminate.php', true);
                        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                        xhr.onreadystatechange = function () {
                            if (xhr.readyState === 4 && xhr.status === 200) {
                                // 移除最后三个选中的卡片
                                selectedCards.splice(-3, 3);
                                const lastThreeElements = selectedCardsDiv.querySelectorAll('.selected:last-child, .selected:nth-last-child(2), .selected:nth-last-child(3)');
                                lastThreeElements.forEach(element => element.remove());
                            }
                        };
                        xhr.send('card=' + lastThree[0]);
                    }
                }
            }
        });
    </script>
</body>

</html>